package queryManager;




import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;

import utils.GenerateId;

/**
 * Parcours le dossier contenant les qrel et stocke les
 * dans une hasmap : <idqrel,<chemin du paragraphe,0 ou 1>>
 * @author amady
 *
 */
public class QrelManager {
	
	private static final String DELIMITER="[ \t]";//espace ou tabulation
	//<idqrel,<chemin du paragraphe,0 ou 1>>
	private HashMap<Integer,HashMap<String ,Integer>> qrels;
	
	public QrelManager(String path,String regexp) throws IOException{
		this.qrels = new HashMap<Integer, HashMap<String,Integer>>();
		
		File[] listFile = GenerateId.findFileListOfDir(path, regexp);
		
		FileReader file = null;
		BufferedReader buffer=null;
		int id = 0;
		int isPertinent = 0;
		String chemin="";
		for(File f: listFile){
			file = new FileReader(f);
			buffer = new BufferedReader(file);
			String line;
			id = Integer.valueOf(f.getName().substring(f.getName().indexOf("qrel")+4, f.getName().indexOf(".txt")));
			this.qrels.put(id, new HashMap<String, Integer>());
			while((line=buffer.readLine())!=null) {
				chemin = line.split(DELIMITER)[0]+line.split(DELIMITER)[1];
				isPertinent = Integer.valueOf(line.split(DELIMITER)[2]);
				if(!this.qrels.get(id).keySet().contains(chemin))
					this.qrels.get(id).put(chemin, isPertinent);
				
			}
//			break;
			buffer.close();
			file.close();
		}
		
		
	}
	
	public HashMap<Integer, HashMap<String, Integer>> getQrels() {
		return qrels;
	}

	public void setQrels(HashMap<Integer, HashMap<String, Integer>> qrels) {
		this.qrels = qrels;
	}
	
	//prend bcp de temps car trop de données
	public String toString(){
		String res = "";
		for(Integer i : this.qrels.keySet()){
			res+="Identifiant: "+ i+"\n";
			for(String s : this.qrels.get(i).keySet()){
				res+=s+"\t"+this.qrels.get(i).get(s)+"\n";
			}
			res+="\n";
//			break;
		}
		return res;
	}


	
	
	

}
